cc.Class({
    extends: cc.Component,

    properties: {
        speed:10,
		bullet: {
			default: null,
			type: cc.Prefab
		}
    },

    // use this for initialization
    onLoad: function () {
		
		var self = this
		self.leftToward = false
		self.rightToward = false
		
		cc.eventManager.addListener({
			event: cc.EventListener.KEYBOARD,
			onKeyPressed:  function(keyCode, event){
				console.log('pressed:'+keyCode)
				switch(keyCode){
					case cc.KEY.a:{
						self.leftToward = true
						break
					}
					case cc.KEY.d:{
						self.rightToward = true
						break
					}
					case cc.KEY.q:{
						//var bullet = cc.instantiate(self.bullet);
						//var canvasNode = self.node.parent;
						//self.node.addChild(bullet)
                        //
						//var moveBy = cc.moveBy(0.5,cc.p(1000,0))
						//var callFunc = cc.callFunc(function(){
						//	bullet.active = false
						//	bullet.removeFromParent(true)
						//	cc.vv.game.bullets.splice(cc.vv.game.bullets.indexOf(bullet),1)
						//},self)
						//var sequence = cc.sequence(moveBy,callFunc)
						//bullet.runAction(sequence)
                        //
						//cc.vv.game.bullets.push(bullet)    预制体 Prefab
						
						
						var bullet = cc.instantiate(self.bullet)
						
						var canvas = self.node.parent;
						canvas.addChild(bullet)
						
						var callback = function(){
							bullet.active = false
							bullet.removeFromParent(true)
						}
						var callFunc = cc.callFunc(callback,self)
						
						var action1 = cc.moveBy(0.4,cc.p(200,100))
						var action2 = cc.moveBy(0.4,cc.p(200,-100))
						var seq = cc.sequence(action1,action2,callFunc)
						
						bullet.runAction(seq)
						break
					}
					default:{
						break
					}
				}
			},
			onKeyReleased: function(keyCode, event){
				self.leftToward = false
				self.rightToward = false
			}
		}, self.node);
    }, 

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
		if(this.leftToward){
				console.log('leftToward')
			this.node.x -= this.speed * dt
			//this.node.position.x = this.node.position.x - this.speed
		}else if(this.rightToward){
				console.log('rightToward')
			this.node.x += this.speed * dt
		}
    },
	
	start: function (dt) {
		console.log('start')
    },
});
